package com.fcat.common.cmd;


import com.fcat.base.common.util.CmdExecUtil;
import com.fcat.base.common.util.StrUtil;
import com.fcat.base.server.util.ShellCommand;

import java.io.IOException;
import java.util.Scanner;

public class CmdBootstrap{
    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner scanner = new Scanner(System.in);
        boolean exit = false;
        while(!exit){
            help();
            System.out.print("选择编号：");
            if (scanner.hasNextLine()){

                String strs = scanner.nextLine();
                if(!StrUtil.isEmpty(strs)){
                    try{
                        String[] ss = strs.split(",");
                        Integer[] iss = new Integer[ss.length];
                        for(int i=0;i<ss.length;i++) {
                            String str = ss[i];
                            Integer num = Integer.valueOf(str);
                            iss[i] = num;
                        }
                        for(int i=0;i<iss.length;i++){
                            Integer num = iss[i];
                            if(num==0){
                                exit = true;
                                break;
                            }
                            switch(num){
                                case 1:
                                    mvnInstall("fcat-common");
                                    break;
                                case 2:
                                    ShellCommand.shellCommandByMeter178().sftpJar("common-monitor");
                                    break;
                                case 3:
                                    ShellCommand.shellCommandByMeter178().excuteShellCmd("cd /docker/springboot/ && docker restart common-monitor\r");
                                    break;
                                case 4:
                                    ShellCommand.shellCommandByMeter178().sftpJar("common-user");
                                    break;
                                case 5:
                                    ShellCommand.shellCommandByMeter178().excuteShellCmd("cd /docker/springboot/ && docker restart common-user\r");
                                    break;
                                case 6:
                                    ShellCommand.shellCommandByMeter178().sftpJar("common-fdfs");
                                    break;
                                case 7:
                                    ShellCommand.shellCommandByMeter178().excuteShellCmd("cd /docker/springboot/ && docker restart common-fdfs\r");
                                    break;
                                case 8:
                                    ShellCommand.shellCommandByMeter178().sftpJar("gateway");
                                    break;
                                case 9:
                                    ShellCommand.shellCommandByMeter178().excuteShellCmd("cd /docker/springboot/ && docker restart gateway\r");
                                    break;
                                case 10:
                                    ShellCommand.shellCommandByMeter178().sftpJar("registry");
                                    break;
                                case 11:
                                    ShellCommand.shellCommandByMeter178().excuteShellCmd("cd /docker/springboot/ && docker restart registry\r");
                                    break;
                                case 98:
                                    mvnInstall("fcat-base");
                                    break;
                                case 99:
                                    mvnInstall("fcat-springboot");
                                    break;
                                case 100:
                                    mvnInstall("fcat-meter");
                                    break;
                                case 101:
                                    ShellCommand.shellCommandByMeter178().sftpJar("meter-hes");
                                    break;
                                case 102:
                                    ShellCommand.shellCommandByMeter178().excuteShellCmd("cd /docker/springboot/ && docker restart meter-hes\r");
                                    break;
                            }
                        }

                    }catch (NumberFormatException e){
                        System.err.println("编号输入有误");
                    }

                }
            }
        }
    }

    private static void help(){
        System.out.println();
        System.out.println("=======================指令,执行多个指令用因为逗号隔开====================================================================================================");
        System.out.println(String.format("%-24s","0.退出"));
        System.out.println();
        System.out.println(String.format("%-24s","98.打包fcat-base"));
        System.out.println(String.format("%-24s","99.打包fcat-springboot"));
        System.out.println();
        System.out.println(String.format("%-24s","1.打包fcat-common"));
        System.out.println(String.format("%-24s%-24s%-24s%-24s%-24s%-24s","2.上传common-monitor.jar","3.重启common-monitor","4.上传common-user.jar","5.重启common-user","6.上传common-fdfs.jar","7.重启common-fdfs"));
        System.out.println(String.format("%-24s%-24s%-24s%-24s%-24s%-24s","8.上传gateway.jar","9.重启gateway","10.上传registry.jar","11.重启registry","",""));
        System.out.println();
        System.out.println(String.format("%-24s","100.打包fcat-meter"));
        System.out.println(String.format("%-24s%-24s%-24s%-24s%-24s%-24s","101.上传meter-hes.jar","102.重启meter-hes","","","",""));
        System.out.println("====================================================================================================================================================");
        System.out.println();
    }
    public static void mvnInstall(String projectRootName) throws IOException, InterruptedException {
        String localProjectPath = System.getProperty("user.dir");
        localProjectPath = localProjectPath.substring(0,localProjectPath.lastIndexOf("\\")+1)+projectRootName;
        CmdExecUtil.cmdExec("cd " + localProjectPath+" && mvn clean install -Dmaven.test.skip=true");
    }

}
 